var config = require("config");
cc.Class({
    extends: cc.Component,

    properties: {
        xMinSpeed: {
            default:0,
            type:cc.Integer,
            tooltip: 'x轴最小速度',
        },
        xMaxSpeed: {
            default:0,
            type:cc.Integer,
            tooltip: 'x轴最大速度',
        },
        yMinSpeed: {
            default:0,
            type:cc.Integer,
            tooltip: 'y轴最小速度',
        },
        yMaxSpeed: { 
            default:0,
            type:cc.Integer,
            tooltip: 'y轴最大速度',
        },
        initHP: {
            default:0,
            type:cc.Integer,
            tooltip: '初始生命值',
        }, 
        initSpriteFrame:{
            default: null,
            type: cc.SpriteFrame,
            tooltip: '初始化的图像'
        }, 
        score: {
            default:0,
            type:cc.Integer,
            tooltip: '死后获得的分数'
        },
        sprArray :{
            default:[],
            type:[cc.SpriteFrame],
        },
        enemyType:{
            default:0,
            type:cc.Integer,
            tooltip: '怪物类型'
        },
        progress_hp: {
            default: null,
            type: cc.ProgressBar
        },
        lab_info:cc.Label,
        // enemyDownClip: cc.AudioClip
    },

    // use this for initialization
    onLoad: function () {
        cc.director.getCollisionManager().enabled=true;
        this.xSpeed = Math.random()*(this.xMaxSpeed-this.xMinSpeed) + this.xMinSpeed;
        this.ySpeed = Math.random()*(this.yMaxSpeed-this.yMinSpeed) + this.yMaxSpeed;//this.yMinSpeed;
        this.ySpeed = this.ySpeed*3;
        this.enemyGroup = this.node.parent.getComponent('enemyGroup');


        if(this.enemyType == 4){
            this.sprIndex = Math.floor(Math.random()*12);
        }

        this.initHP = config.EnemyMaxHp;
        this.initHit = config.EnemyHit;

        this.lab_info.string = "";
    },
    init: function(){
        
        if (this.node.group != 'enemy'){
            this.node.group = 'enemy';
        }
        // if (this.hP != this.initHP){
            this.hP = this.initHP;
            this.setEnemyHp(this.hP);
            this.progress_hp.node.active = false;
            this.initHP = config.EnemyMaxHp;
        // }

        // if (this.hit != this.initHit){
            this.hit = this.initHit;
            this.initHit = config.EnemyHit;
        // }

        // this.lab_info.string = "攻击"+this.hit+"血量"+this.hP;


        var nSprite = this.node.getComponent(cc.Sprite);
        if (nSprite.spriteFrame != this.initSpriteFrame && this.enemyType != 4){
            nSprite.spriteFrame = this.initSpriteFrame;
        }else if(this.enemyType == 4){
            nSprite.spriteFrame = this.sprArray[this.sprIndex];
        }
        
    },
    setEnemyHp: function(hp){
        this.progress_hp.node.active = true;
        let _hp = hp/this.initHP;
        this.progress_hp.progress = _hp;
    },
    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        if (this.enemyGroup.eState != D.commonInfo.gameState.start){
            return ;
        }
        
        if(config.HeroState == 1){ 
            this.node.y += dt*this.ySpeed-40;
        }else if(config.doubleSpeed){
            this.node.y += dt*this.ySpeed-10;
        }else{
            this.node.y += dt*this.ySpeed;
        }

        this.node.x += dt*this.xSpeed;
        //出屏幕后 回收节点
        if (this.node.y < -this.node.parent.height/2){
            this.enemyGroup.enemyDied(this.node,0);
        }
    },
    //碰撞检测
    onCollisionEnter: function(other,self){

        if(other.node.group == 'hero' && config.HeroState == 1){
            this.playDieAni();
        }
        
        if (other.node.group != 'bullet'){
            return ;
        }

        // console.log(">>>other.node",other.node._name)

        if(other.node.name == 'bullet_bb'){
            var bullet = other.node.getComponent('bullet_bb');
        }else if(other.node.name == 'bullet'){
            var bullet = other.node.getComponent('bullet');
        }else{
            var bullet = {hpDrop : 1000000}
        }

        if (this.hP>0){//防止再次碰撞
            this.hP -= bullet.hpDrop;
            this.setEnemyHp(this.hP);
        } else {
            return ;
        }
        if (this.hP <= 0){
            this.node.group = 'default'; //不让动画在执行碰撞
            //播放动画
            var anim = this.getComponent(cc.Animation);
            // var animName = self.node.name + '_ani';
            var animName = 'enemy_ani';
            anim.play(animName);
            anim.on('finished',  this.onFinished, this);
            //播放音效
            cc.vv.audioMgr.playSFX("fire_explo");
            
            this.progress_hp.node.active = false;
            //掉落道具
            var randomNum = Math.floor(Math.random()*100);
            if(randomNum<40){
                var newV2 = cc.v2(this.node.x,this.node.y);
                this.enemyGroup.initProp(newV2);
            }
        }
    },
    //动画结束后 动画节点回收
    onFinished: function(event) { 
        this.enemyGroup.enemyDied(this.node, this.score);
    },
    //播放死亡动画
    playDieAni(){
        this.hP = 0;
        this.node.group = 'default'; //不让动画在执行碰撞
        var anim = this.getComponent(cc.Animation);
        var animName = 'enemy_ani';
        anim.play(animName);
        anim.on('finished',  this.onFinished, this);
        this.progress_hp.node.active = false;

        cc.vv.audioMgr.playSFX("fire_explo");

        //掉落道具
        var newV2 = cc.v2(this.node.x,this.node.y);
        this.enemyGroup.initProp(newV2);
    },
});
